package com.shenghuai.bclient.stores.adapter;

import android.util.SparseArray;
import android.view.View;

import androidx.annotation.IdRes;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

@SuppressWarnings("unchecked")
public class ViewHolder extends RecyclerView.ViewHolder {

    public static final int viewTypeEmpty = -99;

    private static final String TAG = ViewHolder.class.getSimpleName();

    private final SparseArray<View> sparseArray;

    public ViewBinding attachViewBinding;

    public ViewHolder(View itemView) {
        super(itemView);
        sparseArray = new SparseArray<>();
    }

    public void putValue(int key, Object value) {
        itemView.setTag(key, value);
    }

    public <T> T getValue(int key) {
        return (T) itemView.getTag(key);
    }

    public <T extends View> T getView(@IdRes int id) {

        View childView = sparseArray.get(id);
        if (null != childView) {
            return (T) childView;
        }

        childView = itemView.findViewById(id);
        if (null == childView) {
            return null;
        }
        sparseArray.put(id, childView);

        return (T) childView;
    }

    public View getView() {
        return itemView;
    }

}
